/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.DichVuDTO;

/**
 *
 * @author tuan
 */
public class DichVuDAO {
    private static final String MaDichVu = "MaDV0000";
    private static final int lengthMa = 8;
    
    public DichVuDAO() {
    }
    
    public String getNewMaDichVu() throws SQLException {
        String MaDV = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllDichVuFromDatabase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MADICHVU").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaDV = MaDV.concat(MaDichVu + maxMa);
        return MaDV;
    }
    
    public ArrayList<DichVuDTO> getAllDichVu() throws SQLException {
        ArrayList<DichVuDTO> listDV = new ArrayList<>();
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetAllDichVuFromDatabase", null);
        DichVuDTO dichvu = new DichVuDTO();
        while (result.next()) {
            dichvu.setMaDichVu(result.getString("MADICHVU"));
            dichvu.setTenDichVu(result.getString("TENDICHVU"));
            dichvu.setGiaTien(result.getLong("GIATIEN"));
        }
        return listDV;
    }
    
    public DichVuDTO getDichVuByMaDV(String MaDV) throws SQLException {
        Object[] objs = { MaDV };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetDichVuFromDatabase( ? )", objs);
        DichVuDTO dichvu = new DichVuDTO();
        while (result.next()) {
            dichvu.setMaDichVu(result.getString("MADICHVU"));
            dichvu.setTenDichVu(result.getString("TENDICHVU"));
            dichvu.setGiaTien(result.getLong("GIATIEN"));
        }
        return dichvu;
    }
    
    public int updateDichVu(DichVuDTO dv) {
        Object[] objs = { dv.getMaDichVu(), dv.getTenDichVu(), dv.getGiaTien() };
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateDichVuToDatabase(?, ?, ?)", objs);
    }
    
    public int deleteDichVu(DichVuDTO dv) {
        Object[] objs = { dv.getMaDichVu() };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteDivhVuFromDatabase( ? )", objs);
    }
    
    public int insertDichVu(DichVuDTO dv) {
        Object[] objs = { dv.getMaDichVu(), dv.getTenDichVu(), dv.getGiaTien() };
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertDichVuToDatabase(?, ?, ?)", objs);
    }
}
